package com.topisv.tms.webservice.soap;

import javax.jws.WebService;

import org.apache.commons.lang3.Validate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.topisv.tms.entity.Customers;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.edi.ocr.AddOCROrdenoImpl;
import com.topisv.tms.service.edi.ocr.AddOCRSkuImpl;
import com.topisv.tms.webservice.soap.response.GetReturnMessage;
import com.topisv.tms.webservice.soap.response.base.WSResult;
import com.topisv.tms.webservice.soap.response.vo.ReturnMessage;

@WebService(serviceName = "AddMessageService", endpointInterface = "com.topisv.tms.webservice.soap.IAddMessageService", targetNamespace = WsConstants.NS)
public class AddMessageServiceImpl implements IAddMessageService {
	private static final Logger logger = LoggerFactory .getLogger(AddMessageServiceImpl.class);
	
	@Autowired
	private AddOCROrdenoImpl addOrdenoImpl;

	@Autowired
	private AddOCRSkuImpl addSkuImpl;

	@Autowired
	private CustomersService customersService;

	public AddMessageServiceImpl() {
	}

	public GetReturnMessage addOrdeno(String username, String password, String json) {
		GetReturnMessage result = new GetReturnMessage();
		ReturnMessage returnMessage = null;
		
		try {
			logger.info("username==" + username);
			logger.info("password==" + password);
			logger.info("json==" + json);
			logger.info("addOrdeno==" + json);
			Validate.notNull(username, "用户名参数为空");
			Validate.notNull(password, "密码参数为空");
			Validate.notNull(json, "json参数参数为空");
			Customers customers = customersService.findByCustomerCode(username);
			if (customers == null) {
				result.setCode(result.PARAMETER_ERROR);
				result.setMessage("验证错误");
				return result;
			}
			returnMessage = addOrdenoImpl.addOCROrdeno(username, password, json);
			if ("1".equals(returnMessage.getStatus())) {
				result.setCode(result.SUCESS);
				result.setMessage("接收成功");
			} else if ("-1".equals(returnMessage.getStatus())) {

				result.setCode(result.PARAMETER_ERROR);
				result.setMessage("验证错误");
			}
		} catch (Exception e) {
			
			logger.error("some error happened",e);
			return handleParameterError(result, e);

		}
		logger.info(result.toString());
		return result;
	}

	public GetReturnMessage addSku(String username, String password, String json) {

		GetReturnMessage result = new GetReturnMessage();
		ReturnMessage returnMessage = null;
		try {
			logger.info("username==" + username);
			logger.info("password==" + password);
			logger.info("json==" + json);
			logger.info("addSku==" + json);
			Customers customers = customersService.findByCustomerCode(username);
			if (customers == null) {
				result.setCode(result.PARAMETER_ERROR);
				result.setMessage("验证错误");
				return result;
			}
			returnMessage = addSkuImpl.addOCRSku(username, password, json);
			if ("1".equals(returnMessage.getStatus())) {
				result.setCode(result.SUCESS);
				result.setMessage("接收成功");
			} else if ("-1".equals(returnMessage.getStatus())) {

				result.setCode(result.PARAMETER_ERROR);
				result.setMessage("验证错误");
			}
		} catch (Exception e) {
			
			logger.error("addSku case some error",e);
			return handleParameterError(result, e);

		}
		return result;
	}

	private GetReturnMessage handleParameterError(GetReturnMessage result,
			Exception e, String message) {
		logger.error(message, e.getMessage());
		result.setError(WSResult.PARAMETER_ERROR, message);
		return result;
	}

	private GetReturnMessage handleParameterError(GetReturnMessage result,
			Exception e) {
		logger.error("error",e);
		result.setError(WSResult.PARAMETER_ERROR, e.getMessage());
		return result;
	}

	private GetReturnMessage handleGeneralError(GetReturnMessage result,
			Exception e) {
		logger.error("error",e);
		result.setDefaultError();
		return result;
	}

}
